package org.foreveross.que.web.action.help;


import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.foreveross.que.application.TreeMenuApplication;
import org.foreveross.que.entity.MenuEntity;
import org.foreveross.que.vo.TreeChildVo;
import org.foreveross.que.vo.TreeManagerVo;
import org.foreveross.que.web.action.BaseAction;

@SuppressWarnings("serial")
@Controller
@org.springframework.context.annotation.Scope("prototype")
@Namespace("/help/treeManager")
@ParentPackage("json-default")
public class TreeManagerAction  extends BaseAction {
	@Autowired 
	TreeMenuApplication application;
	
	TreeManagerVo vo = new TreeManagerVo();
	
	
	@Action(value="toJsp",results={@Result(name="dispatcher", type="dispatcher",location="/pages/help/tree_manager.jsp")})
	public String toJsp()throws Exception{
		vo.setMenus(fromObject(getTopTrees()));
		return "dispatcher";
	}
	
	@Action(value="toEdit",results={@Result(name="dispatcher", type="dispatcher",location="/pages/help/tree_edit.jsp")})	
	public String toEdit() throws Exception{
		if(StringUtils.isNotBlank(vo.getSysType())){
		if(StringUtils.equals(vo.getSysType(), vo.getMid())){
			TreeChildVo chileVo = this.application.getSysChildVo(vo.getSysType(), null);
			vo.setNode(chileVo);
			List<TreeChildVo> treeR = new ArrayList<TreeChildVo>();
			treeR.add(chileVo);
			vo.setTreeR(treeR);
		}else if(!vo.getMid().equals(vo.getSysType())){
		vo.setEntity(this.application.findEntity(vo.getMid()));
		List<TreeChildVo> treeR = this.application.loadTreeR(vo.getMid(), vo.getSysType());
		vo.setTreeR(treeR);
		vo.setNode(treeR.get(treeR.size()-1));
		}
		if(vo.getTreeR()!=null&&vo.getTreeR().size()>=2){
			vo.setParentNode(vo.getTreeR().get(vo.getTreeR().size()-2));
		}
		}
		return  "dispatcher";
	}
	@Action(value="toAddMenu",results={@Result(name="dispatcher", type="dispatcher",location="/pages/help/tree_edit.jsp")})
	public String toAddMenu() throws Exception {
		if(StringUtils.equals(vo.getMid(), vo.getSysType())){
			this.vo.getParentNode().setChildren(this.application.getMenuTree(vo.getMid()));
		}else{
			this.vo.setParentNode(this.application.getTreeById(vo.getMid(), vo.getSysType()));
			vo.getNode().setPid(vo.getMid());
		}
		
		vo.setAction("addMenu");
		return  "dispatcher";
	}
	
	
	@Action(value="addMenu",results={@Result(name="dispatcher", type="dispatcher",location="/pages/help/tree_edit.jsp")})	
	public String addMenu() throws Exception{
		MenuEntity entity = this.application.addMenu(vo);
		vo.setMid(entity.getCode());
		vo.setAction("updateMenu");
		return this.toEdit();
	}
	@Action(value="updateMenu",results={@Result(name="dispatcher", type="dispatcher",location="/pages/help/tree_edit.jsp")})	
	public String updateMenu() throws Exception{
		MenuEntity entity = this.application.update(vo);
		vo.setMid(entity.getCode());
		return this.toEdit();
	}
	
	@Action(value="addSys",results={@Result(name="dispatcher", type="dispatcher",location="/pages/help/tree_edit.jsp")})	
	public String addSys() throws Exception{
		this.application.addSys(vo);
		vo.setSysType(vo.getSysEntity().getSysType());
		vo.setMid(vo.getSysType());
		return this.toEdit();
	}
	
	public String deleteMenus() throws Exception{
		return this.toEdit();
	}
		
	
	private List<TreeChildVo> getTopTrees() throws Exception{
		List<TreeChildVo> parents = application.getAllMenuTree();
		return parents;
	}

	public TreeManagerVo getVo() {
		return vo;
	}


	public void setVo(TreeManagerVo vo) {
		this.vo = vo;
	}
	

	
	

}
